package com.cdn.nodes.controller;

import com.cdn.common.core.web.controller.BaseController;
import com.cdn.common.core.web.domain.AjaxResult;
import com.cdn.common.core.web.page.TableDataInfo;
import com.cdn.common.log.annotation.Log;
import com.cdn.common.log.enums.BusinessType;
import com.cdn.common.security.annotation.RequiresPermissions;
import com.cdn.nodes.domain.EdgeClusters;
import com.cdn.nodes.domain.dto.EdgeClustersAddDTO;
import com.cdn.nodes.domain.vo.EdgeClustersDnsVO;
import com.cdn.nodes.service.IEdgeClustersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.List;

/**
 * 边缘集群，存储边缘集群的相关信息，包括配置和状态Controller
 * 
 * @author Cinzia
 * @date 2024-12-14
 */
@RestController
@RequestMapping("/clusters")
public class EdgeClustersController extends BaseController
{
    @Resource
    private IEdgeClustersService edgeClustersService;

    /**
     * 查询边缘集群，存储边缘集群的相关信息，包括配置和状态列表
     */
    @RequiresPermissions("nodes:clusters:list")
    @GetMapping("/list")
    public TableDataInfo list(EdgeClusters edgeClusters)
    {
        startPage();
        List<EdgeClusters> list = edgeClustersService.selectEdgeClustersList(edgeClusters);
        return getDataTable(list);
    }

//    /**
//     * 导出边缘集群，存储边缘集群的相关信息，包括配置和状态列表
//     */
//    @RequiresPermissions("nodes:clusters:export")
//    @Log(title = "边缘集群，存储边缘集群的相关信息，包括配置和状态", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, EdgeClusters edgeClusters)
//    {
//        List<EdgeClusters> list = edgeClustersService.selectEdgeClustersList(edgeClusters);
//        ExcelUtil<EdgeClusters> util = new ExcelUtil<EdgeClusters>(EdgeClusters.class);
//        util.exportExcel(response, list, "边缘集群，存储边缘集群的相关信息，包括配置和状态数据");
//    }

    /**
     * 获取边缘集群，存储边缘集群的相关信息，包括配置和状态详细信息
     */
    @RequiresPermissions("nodes:clusters:query")
    @GetMapping(value = "/{clusterId}")
    public AjaxResult getInfo(@PathVariable("clusterId") Long clusterId)
    {
        return success(edgeClustersService.selectEdgeClustersByClusterId(clusterId));
    }

    /**
     * 新增边缘集群，存储边缘集群的相关信息，包括配置和状态
     */
    @RequiresPermissions("nodes:clusters:add")
    @Log(title = "边缘集群，存储边缘集群的相关信息，包括配置和状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EdgeClustersAddDTO edgeClustersAddDTO)
    {
        return toAjax(edgeClustersService.insertEdgeClusters(edgeClustersAddDTO));
    }

    /**
     * 修改边缘集群，存储边缘集群的相关信息，包括配置和状态
     */
    @RequiresPermissions("nodes:clusters:edit")
    @Log(title = "边缘集群，存储边缘集群的相关信息，包括配置和状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EdgeClusters edgeClusters)
    {
        return toAjax(edgeClustersService.updateEdgeClusters(edgeClusters));
    }

    /**
     * 删除边缘集群，存储边缘集群的相关信息，包括配置和状态
     */
    @RequiresPermissions("nodes:clusters:remove")
    @Log(title = "边缘集群，存储边缘集群的相关信息，包括配置和状态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clusterIds}")
    public AjaxResult remove(@PathVariable Long[] clusterIds)
    {
        return toAjax(edgeClustersService.deleteEdgeClustersByClusterIds(clusterIds));
    }

    /**
     * 根据集群Id获取所有节点
     */
    @GetMapping("/getNodesByClusterId/{cluster}")
    public AjaxResult getByClusterId(@PathVariable Long cluster) throws ServerException {
        return success(edgeClustersService.getByClusterId(cluster));
    }

    /**
     * 根据集群Id获取集群Dns
     */
    @GetMapping("/getDnsByClusterId/{cluster}")
    public AjaxResult getDnsByClusterId(@PathVariable Long cluster) throws ServerException {
        return success(edgeClustersService.getDnsByClusterId(cluster));
    }

    /**
     * 根据集群Id获取集群ssh认证
     */
    @GetMapping("/getClusterSsh/{cluster}")
    public AjaxResult getClusterSsh(@PathVariable Long cluster) throws ServerException {
        return success(edgeClustersService.getClusterSsh(cluster));
    }

    /**
     * 根据集群获取域名信息
     */
    @GetMapping("/getClusters/dnsInfo/list")
    public TableDataInfo getClustersDnsInfo(String keyword) throws ServerException {
        startPage();
        List<EdgeClustersDnsVO> list = edgeClustersService.getClustersDnsInfo(keyword);
        return getDataTable(list);
    }

    /**
     * 根据集群获取域名信息
     */
    @GetMapping("/getClusters/dnsInfo/{clusterId}")
    public EdgeClustersDnsVO getClustersDnsInfoByClusterId(@PathVariable Long clusterId) throws ServerException {
        return edgeClustersService.getClustersDnsInfoByClusterId(clusterId);
    }

    /**
     * 根据节点ID获取所属集群
     */
    @GetMapping("/getClusterByNodeId/{nodeId}")
    public String getClusterByNodeId(@PathVariable Long nodeId) throws ServerException {
        return edgeClustersService.getClusterByNodeId(nodeId);
    }

//    /**
//     * 根据集群ID查询该集群可用DNS线路
//     */
//    @GetMapping("/getDnsLine/{clusterId}")
//    public String getDnsLine(@PathVariable Long clusterId) throws ServerException {
//        return edgeClustersService.getDnsLine(clusterId);
//    }

    @GetMapping("/getInstallPath/{nodeId}")
    public String getInstallPath(@PathVariable Long nodeId) throws ServerException {
        return edgeClustersService.getInstallPath(nodeId);
    }


}
